package de.revdev.llt.bean.managed.converter;

import de.revdev.llt.bean.entities.Category;
import de.revdev.llt.bean.facade.CategoryFacade;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
/**
 *
 * @author f.lindermaier
 */
@ManagedBean(name = "CategoryConverter")
public class CategoryConverter implements Converter {

    private CategoryFacade cf;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        Long id = new Long(value);
        Category wa = cf.find(id.longValue());
        return wa;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value.toString().equals("")) {
            return "";
        }
        Long id = ((Category) value).getId();
        return id.toString();
    }

    @Inject
    public void setCategoryFacade(CategoryFacade categoryFacade) {
        cf = categoryFacade;
    }
}
